﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Security.Cryptography;
using Ext.Net;
using System.Web.UI;
using System.Web.Security;
using System.Web;
//using Viewer.BusinessObject.BusinessEntities;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class QUNguoiDung : BusinessBase
    {
        public List<QUNguoiDung> LoadData()
        {
            if (HttpContext.Current.User.Identity.Name.ToLower() == "admin")
            {

                return this.Context.QUNguoiDungs.ToList();
            }
            else
            {
                List<QUNguoiDung> nguoiDung = SearchNguoiDung(HttpContext.Current.User.Identity.Name.ToLower());

                var lstNhomNguoiDung = (from nnd_nd1 in this.Context.QUNhomNguoiDung_NguoiDungs
                                        where nnd_nd1.NguoiDungID == nguoiDung[0].NguoiDungID
                                        select nnd_nd1.NhomNguoiDungID).Distinct();
                List<string> nhomNguoiDung = new List<string>();

                foreach (var item in lstNhomNguoiDung)
                {
                    nhomNguoiDung.Add(item.ToString());
                }

                var lstNguoiDung = (from nd in this.Context.QUNguoiDungs
                                    join nnd_nd in this.Context.QUNhomNguoiDung_NguoiDungs on nd.NguoiDungID equals nnd_nd.NguoiDungID
                                    join nnd in this.Context.QUNhomNguoiDungs
                                    on nnd_nd.NhomNguoiDungID equals nnd.NhomNguoiDungID
                                    where nhomNguoiDung.Contains(nnd.NhomNguoiDungID.ToString()) 
                                    select nd)
                                    .Union
                                    (from nd2 in this.Context.QUNguoiDungs
                                     where nd2.NguoiDungID == nguoiDung[0].NguoiDungID select nd2).ToList();

                return lstNguoiDung;
            }
        }
        public QUNguoiDung LoadDataById(int id)
        {
            return this.Context.QUNguoiDungs.Where(a => a.NguoiDungID.Equals(id)).SingleOrDefault();
        }
        /// <summary>
        /// Updates the nguoi dung.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="ten">The ten.</param>
        /// <param name="matkhau">The matkhau.</param>
        /// <author>baodt</author>
        /// <datetime>6/30/2011-10:03 AM</datetime>
        public void UpdateNguoiDung(int id, string ten, string matkhau)
        {
            using (var context = new DataClassesDataContext())
            {
                var item = context.QUNguoiDungs.Single(a => a.NguoiDungID.Equals(id));
                item.MatKhau = matkhau;
                item.TenDangNhap = ten;
                context.SubmitChanges();
            }
        }
        /// <summary>
        /// Deletes the nguoi dung.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="ten">The ten.</param>
        /// <param name="matkhau">The matkhau.</param>
        /// <author>baodt</author>
        /// <datetime>6/30/2011-10:03 AM</datetime>
        public void DeleteNguoiDung(int id)
        {
            using (var context = new DataClassesDataContext())
            {
                //xóa tất cả các bảng liên quan
                var item = context.QUNguoiDungs.Single(a => a.NguoiDungID.Equals(id));
                context.QUNguoiDungs.DeleteOnSubmit(item);
                var itemToChuc = context.QUNhomQuyenByToChuc_NguoiDungs.Where(a => a.NguoiDungID.Equals(id));
                context.QUNhomQuyenByToChuc_NguoiDungs.DeleteAllOnSubmit(itemToChuc);
                var itemModule = context.QUNhomQuyenByModule_NguoiDungs.Where(a => a.NguoiDungID.Equals(id));
                context.QUNhomQuyenByModule_NguoiDungs.DeleteAllOnSubmit(itemModule);
                var itemNhomNguoiDung = context.QUNhomNguoiDung_NguoiDungs.Where(a => a.NguoiDungID.Equals(id));
                context.QUNhomNguoiDung_NguoiDungs.DeleteAllOnSubmit(itemNhomNguoiDung);
                context.SubmitChanges();
            }
        }
        /// <summary>
        /// Inserts the nguoi dung.
        /// </summary>
        /// <param name="ten">The ten.</param>
        /// <param name="matkhau">The matkhau.</param>
        /// <author>baodt</author>
        /// <datetime>6/30/2011-4:12 PM</datetime>
        public int InsertNguoiDung(string ten, string matkhau)
        {
            using (var context = new DataClassesDataContext())
            {
                QUNguoiDung item = new QUNguoiDung();
                item.TenDangNhap = ten;
                item.MatKhau = matkhau;
                context.QUNguoiDungs.InsertOnSubmit(item);
                context.SubmitChanges();
                return item.NguoiDungID;
            }
        }
        /// <summary>
        /// Kiểm tra tồn tại tên người dùng
        /// </summary>
        /// <param name="ten"></param>
        /// <returns></returns>
        public bool CheckUserExist(string ten)
        {
            List<QUNguoiDung> user = this.Context.QUNguoiDungs
                .Where(a => !string.IsNullOrEmpty(ten) ? a.TenDangNhap.Trim().ToLower().Equals(ten.Trim().ToLower()) : true).ToList();
            if (user.Count() > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// Searches the nguoi dung.
        /// </summary>
        /// <param name="ten">The ten.</param>
        /// <returns></returns>
        /// <author>baodt</author>
        /// <datetime>6/30/2011-4:12 PM</datetime>
        public List<QUNguoiDung> SearchNguoiDung(string ten)
        {
            return this.Context.QUNguoiDungs
                .Where(a => !string.IsNullOrEmpty(ten) ? a.TenDangNhap.ToLower().Contains(ten.ToLower()) : true).ToList();
        }
        public List<QUNguoiDung> GetAll()
        {
            return this.Context.QUNguoiDungs.ToList();
        }

        public List<QUNguoiDung> GetBy_NguoiDungByToChucId(int pNhomQuyenByToChucId)
        {

            var lstExistingNguoiDung = (from c in this.Context.QUNhomQuyenByToChuc_NguoiDungs
                                        where c.NhomQuyenByToChucID == pNhomQuyenByToChucId
                                        select c.NguoiDungID).ToList();
            var query = this.Context.QUNguoiDungs.Where(c => lstExistingNguoiDung.Contains(c.NguoiDungID)).ToList();
            return query;
        }

        public List<QUNguoiDung> GetBy_NguoiDungByModuleId(int pNhomQuyenByModuleID)
        {

            var lstExistingNguoiDung = (from c in this.Context.QUNhomQuyenByModule_NguoiDungs
                                        where c.NhomQuyenByModuleID == pNhomQuyenByModuleID
                                        select c.NguoiDungID).ToList();
            var query = this.Context.QUNguoiDungs.Where(c => lstExistingNguoiDung.Contains(c.NguoiDungID)).ToList();
            return query;
        }

        public bool ValidateUser(string userName, string password)
        {
            //string md5Password = Md5AddSecret(password);
            string md5Password = password;
            var oNguoiDung = (from m in this.Context.QUNguoiDungs
                              where (m.TenDangNhap.Equals(userName) && m.MatKhau.Equals(md5Password))
                              select m).FirstOrDefault();
            if (oNguoiDung == null) return false;
            return true;
        }

        private string Md5AddSecret(string strChange)
        {
            byte[] pass = Encoding.UTF8.GetBytes(strChange);
            MD5 md5 = new MD5CryptoServiceProvider();
            string strPassword = Encoding.UTF8.GetString(md5.ComputeHash(pass));
            return strPassword;
        }

        /// <summary>
        /// Lay danh sach cac NhomQuyenByToChuc_ToChuc lien quan den nguoi dung hien tai
        /// </summary>
        /// <returns></returns>
        public List<QUNhomQuyenByToChuc_ToChuc> NhomQuyenByToChuc_ToChuc()
        {
            //Lay danh sach NhomQuyenByToChuc
            var lstNhomQuyenByToChuc = (from nhomquyen in this.Context.QUNhomQuyenByToChucs
                                        join m in this.Context.QUNhomQuyenByToChuc_NguoiDungs
                                        on nhomquyen.NhomQuyenByToChucID equals m.NhomQuyenByToChucID
                                        where m.NguoiDungID == this.NguoiDungID
                                        select nhomquyen.NhomQuyenByToChucID).ToList();
            //Lay danh sach NhomQuyenByToChuc_ToChuc
            var result = (from m in this.Context.QUNhomQuyenByToChuc_ToChucs
                          where lstNhomQuyenByToChuc.Contains(m.NhomQuyenByToChucID)
                          select m).ToList();

            return result;
        }

        /// <summary>
        /// Lay danh sach cac NhomQuyenByModule_Module lien quan den nguoi dung hien tai
        /// </summary>
        /// <returns></returns>
        public List<QUNhomQuyenByModule_Module> NhomQuyenByModule_Module()
        {
            //Lay danh sach NhomQuyenByMOdule gan voi nguoi dung hien tai
            var lstNhomQuyenByModule = (from nhomquyen in this.Context.QUNhomQuyenByModules
                                        join m in this.Context.QUNhomQuyenByModule_NguoiDungs
                                        on nhomquyen.NhomQuyenByModuleID equals m.NhomQuyenByModuleID
                                        where m.NguoiDungID == this.NguoiDungID
                                        select nhomquyen.NhomQuyenByModuleID).ToList();
            //Lay danh sach NhomQuyenByModule_Module
            var result = (from m in this.Context.QUNhomQuyenByModule_Modules
                          where lstNhomQuyenByModule.Contains(m.NhomQuyenByModuleID)
                          select m).ToList();

            return result;

        }


        /// <summary>
        /// Get NguoiDung By TenDangNHap
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static QUNguoiDung GetByUserName(string username)
        {
            var context = new DataClassesDataContext();
            return context.QUNguoiDungs.Where(c => c.TenDangNhap.ToLower().Equals(username)).FirstOrDefault();
        }
    }
}